#pragma once

#include "stdafx.h"
#include "..\3rdLib\Ogitor\Ogitors.h"
#include "WinSystem.h"
#include <direct.h>
#include "errno.h"

#include "GraphicScriptBinder.h"

//------------------------------------------------------------------------------
CWinSystem::CWinSystem(RenderWindow *window)
{
	pWindow = window;
	OverlayManager& omgr = OverlayManager::getSingleton();
	mLoadOverlay = (Overlay*)omgr.getByName("Race/LoadingOverlay");
	if (!mLoadOverlay)
	{
		OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, 
			"Cannot find loading overlay", "ExampleLoadingBar::start");
	}
	mLoadingBarElement = omgr.getOverlayElement("Race/LoadingBar");
	mLoadingBarElement->setWidth(0);
	mLoadOverlay->hide();

}
//-------------------------------------------------------------------------------
CWinSystem::~CWinSystem()
{
}
//-------------------------------------------------------------------------------
void CWinSystem::GetFileList(Ogre::String path, Ogre::StringVector &list)
{
	Ogre::String pathValue = ExtractFilePath(path);

	list.clear();

	BOOL result = TRUE;
	WIN32_FIND_DATAA finfo;
	HANDLE hDir;

	hDir = FindFirstFileA(path.c_str(), &finfo);
	if (hDir == INVALID_HANDLE_VALUE) return;
	while (result) 
	{
		if (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			result = FindNextFileA(hDir, &finfo);
		}
		else
		{
			list.push_back(pathValue + finfo.cFileName);
			result = FindNextFileA(hDir, &finfo); 
		}
	} 
	FindClose(hDir);
}
//-------------------------------------------------------------------------------
Ogre::String CWinSystem::GetExePath()
{
	char buffer[1000];
	GetModuleFileNameA(NULL,buffer,1000);
	return ExtractFilePath(buffer);
}
//-------------------------------------------------------------------------------
Ogre::String CWinSystem::getProjectsDirectory()
{
	// yeah, I know ...
	char buffer[1000];
	GetModuleFileNameA(NULL,buffer,1000);
	return ExtractFilePath(buffer);
}

void CWinSystem::UpdateLoadProgress(float percentage, Ogre::UTFString msg)
{
	pWindow->update();
	if(percentage<0)
	{
		mLoadOverlay->hide();
	}
	else if(percentage == 100)
	{
		mLoadOverlay->hide();
	}
	else
	{
		mLoadOverlay->show();
		mLoadingBarElement->setWidth(204*percentage/100);
	}

}
